package com.bluefox.hospital.convertor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.bluefox.common.web.dto.AbstractConvertor;
import com.bluefox.hospital.dto.LeaderDTO;
import com.bluefox.hospital.model.Leader;
import com.bluefox.hospital.service.LeaderService;
import lombok.NonNull;

/**
 * LeaderConvertor
 */
@Component
public class LeaderConvertor extends AbstractConvertor<Leader, LeaderDTO> {

    @Autowired
    private LeaderService leaderService;

    @Override
    public Leader toModel(@NonNull final LeaderDTO dto) {
        if (dto.isNew()) {//新增
            return constructModel(dto);
        } else {//更新
            return updateModel(dto);
        }
    }

    @Override
    public LeaderDTO toDTO(@NonNull final Leader model, final boolean forListView) {
        final LeaderDTO dto = new LeaderDTO();
        dto.setId(model.getId());
        dto.setName(model.getName());
        dto.setTitle(model.getTitle());
        dto.setResearch(model.getResearch());
        dto.setIntroduce(model.getIntroduce());

        return dto;
    }

    // 构建新Model
    private Leader constructModel(final LeaderDTO dto) {
        Leader model = new Leader();
        model.setName(dto.getName());
        model.setTitle(dto.getTitle());
        model.setResearch(dto.getResearch());
        model.setIntroduce(dto.getIntroduce());

        return model;
    }

    // 更新Model
    private Leader updateModel(final LeaderDTO dto) {
        Leader model = leaderService.get(dto.getId());
        model.setName(dto.getName());
        model.setTitle(dto.getTitle());
        model.setResearch(dto.getResearch());
        model.setIntroduce(dto.getIntroduce());

        return model;
    }
}
